package com.swz.dcrm.ui.mine;


import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.swz.dcrm.adpter.GlobalAdapter;
import com.swz.dcrm.adpter.GroupAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.Group;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.http.Body;

import com.swz.dcrm.R;
import com.swz.dcrm.ui.viewmodel.GroupViewModel;
import com.swz.dcrm.util.Tool;
import com.swz.dcrm.wrapper.EmptyWrapper;
import com.xh.baselibrary.callback.OnClickListener;


import javax.inject.Inject;

/**
 * A simple {@link Fragment} subclass.
 */
public class GroupManagementFragment extends BaseFragment {

    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.rv)
    RecyclerView rv;

    private int page=1;
    private int size=15;
    private int total;
    private EmptyWrapper emptyWrapper;
    @Inject
    GroupViewModel groupViewModel;

    private GroupAdapter groupAdapter;
    public static GroupManagementFragment newInstance() {

        return new GroupManagementFragment();
        // Required empty public constructor
    }

    @Override
    public void onLoadRetry() {
        if (!isNetworkConnected(getContext())){
            mHolder.showLoadingStatus(GlobalAdapter.NET_ERROR);
            return;
        }
        groupViewModel.getGroups(page,size)
                .observe(this, observer);

    }
    Observer<BaseResponse<Page<Group>>> observer=new Observer<BaseResponse<Page<Group>>>() {
        @Override
        public void onChanged(BaseResponse<Page<Group>> pageBaseResponse) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.setEnableLoadmore(true);
            smartRefreshLayout.finishLoadmore();
            if(pageBaseResponse.isSuccess()){
                total=pageBaseResponse.getData().getTotal();
                if(groupAdapter==null){
                    smartRefreshLayout.setEnableLoadmore(true);
                    groupAdapter=new GroupAdapter(getContext(),pageBaseResponse.getData().getList());
                    groupAdapter.setOnClickListener(onClickListener);
                    emptyWrapper=getEmptyWrapper(groupAdapter,R.mipmap.empty_team);
                    rv.setAdapter(emptyWrapper);
                }else {
                    groupAdapter.refresh(pageBaseResponse.getData().getPageNum(),pageBaseResponse.getData().getList());
                    emptyWrapper.notifyDataSetChanged();
                }
            }
        }
    };

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public int layoutId() {
        return R.layout.fragment_group_management;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        rv.setLayoutManager(new LinearLayoutManager(getContext()));
        title.setText(getString(R.string.group_management_title));
        tvRight.setText(getString(R.string.group_management_add));
        smartRefreshLayout.setOnRefreshListener(refreshlayout -> {
           page=1;
           onLoadRetry();
           smartRefreshLayout.setEnableLoadmore(true);
        });
        smartRefreshLayout.setOnLoadmoreListener(refreshlayout -> {
            if(groupAdapter!=null&&groupAdapter.getItemCount()>=total){
                smartRefreshLayout.finishLoadmore();
                smartRefreshLayout.setEnableLoadmore(false);
                return;
            }else {
                page++;
                onLoadRetry();
            }
        });
        groupViewModel.getAddGroup().observe(this,groupBaseResponse -> {
            if(groupBaseResponse.isSuccess()){
                page=1;
                onLoadRetry();
                smartRefreshLayout.setEnableLoadmore(true);
            }
        });
        groupViewModel.getDelGroup().observe(this,groupBaseResponse -> {
            if(groupBaseResponse.isSuccess()){
                page=1;
                onLoadRetry();
                smartRefreshLayout.setEnableLoadmore(true);
            }
        });
        groupViewModel.getEditGroupResult().observe(this, group -> {
            if(groupAdapter!=null){
                page=1;
                onLoadRetry();
                smartRefreshLayout.setEnableLoadmore(true);
            }
        });
        onLoadRetry();
        return true;
    }


    @OnClick(R.id.iv_back)
    public void backClick() {
        NavHostFragment.findNavController(this).navigateUp();
    }

    @OnClick(R.id.tv_right)
    public void add(){
        go(null,R.id.action_groupManagementFragment_to_editGroupFragment,null);
    }
    OnClickListener<Group> onClickListener= group -> {
        EditGroupFragmentArgs.Builder builder=new EditGroupFragmentArgs.Builder();
        builder.setGroup(new Gson().toJson(group));
        go(null,R.id.action_groupManagementFragment_to_editGroupFragment,builder.build().toBundle());
    };
}
